import express from "express";
import dbhelp from "./../../lib/dahelper";
import async from 'async';
import formidable from "formidable";
import path from "path";
import fs from "fs";
import multiparty from "multiparty";
// const formidable = require('formidable');
// const path = require('path');

// const fs = require('fs');
let clientRouter = express.Router();

// const uploadController = require('./../../controller/uploadController.js');


clientRouter.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", ' 3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
})
// clientRouter.route("/uploadImage").post(uploadController.uploadImage);
clientRouter.post("/uploadImage", 
(request, response, next) => {
    //     uploadImage:function (req,res) {
        // let form = new formidable.IncomingForm();
        // console.log("request",request.files[0]);
        const url=path.join(__dirname+"./../../public/upload/");
fs.readdir(url, 'utf8', (err, fileList) => {
    // console.log(fileList);
    if (err) {
        console.log(err);
    }
    fileList.forEach((item,index)=>{
         let length = item.split('.').length;
         if(length==1)
         {
              let newName = item+".png";
         let oldName = item;
        // console.log(url);
        fs.rename(url+oldName,url+newName,(err)=>{
           console.log(err);
        });
         }    
    })
})
response.json({
            data:request.files[0].filename
        })
})

clientRouter.post("/res", (request, response, next) => {
    // console.log(request.body);
    let czsql="SELECT * FROM USER WHERE u_name=? OR u_email=?";
    let params=[request.body.u_name,request.body.u_email];
    dbhelp.query(czsql,params,(err,result)=>
       {
          let sum=0;
          for(let i=0;i<result.length;i++)
          {
              sum++;
          }
          if(sum>=1)
          {
            response.json({
                msg:"用户名或邮箱已存在",
                status:-1
            })
          }
          else{
                  let params = [request.body.u_name, request.body.u_email,request.body.u_sex,request.body.u_password,request.body.u_number,request.body.u_age,request.body.u_role];
    let res_sql = "INSERT INTO USER (u_name,u_email,u_sex,u_password,u_number,u_age) VALUES (?,?,?,?,?,?)";
    dbhelp.query(res_sql,params, (err, reslut1) => {
    //    console.log(reslut1);
       if(reslut1.affectedRows>=1)
       {
        response.json({
            msg:"注册成功",
            status:1
        })
       }
       else{
        response.json({
            msg:"注册失败",
            status:0
        })
       }
  })
          }
       })
    })
    clientRouter.post("/hshop",(request,response,next)=>
    {
        // let hshopsql="SELECT hs_img FROM hot_shop";
        // dbhelp.query(hshopsql,[],(err,result)=>
        // {
        //     if(err)
        //     {
        //         response.json({
        //             err
        //           })
        //     }
        //     else{
        //         // console.log(result);
        //         response.json({
        //             img:result,
        //             status: 1
        //           })
        //     }
        // })
       
        function fn1(cb)
        {
            let hshopsql="SELECT hs_img FROM hot_shop";
           dbhelp.query(hshopsql,[],(err,reslut)=>{
               console.log("result",reslut)
              cb(err,reslut);
           }
           )
        }
        // let params1=[request.body.c_id,request.body.u_id,request.body.cc_num,request.body.cc_name,request.body.cc_price,request.body.cc_img,request.body.cc_time,request.body.cc_address];
        function fn2(cb)
        {
            let topaysql="SELECT p_id,p_img,p_name,p_price FROM `hs_product` where p_recommend=1";
           dbhelp.query(topaysql,[],(err,reslut)=>{
               console.log(reslut)
              cb(err,reslut);
           }
           )
        }
        function fn3(cb)
        {
            let recommend2sql="SELECT p_id,p_img,p_name,p_price,p_detailes FROM `hs_product` where p_recommend=2";
           dbhelp.query(recommend2sql,[],(err,reslut)=>{
               console.log(reslut)
              cb(err,reslut);
           }
           )
        }
        function fn4(cb)
        {
            let chotsql="SELECT c_id,cs_img,c_detailes,c_name,c_support,c_arrive,c_targetmoney,c_price FROM `crowd_product` WHERE c_hot=1";
           dbhelp.query(chotsql,[],(err,reslut)=>{
               console.log(reslut)
              cb(err,reslut);
           }
           )
        }
        function fn5(cb)
        {
            let zongsql="SELECT p_id,p_img,p_name,p_price,p_detailes FROM `hs_product` LIMIT 0,36";
            dbhelp.query(zongsql,[],(err,reslut)=>{
                console.log(reslut)
               cb(err,reslut);
            })
        }
        async.parallel([fn1,fn2,fn3,fn4,fn5],(err,val)=>
      {
          if(err)
          {
              response.json({
                  err
              })
          }
          else{
            response.json({val:val});
          }
        //   console.log("val",val);
      })
    })
    clientRouter.post("/hshopid",(request,response,next)=>
    {
        let hshopidsql="SELECT hs_id FROM hot_shop WHERE hs_img=?";
       let params=[request.body.hs_img]
        dbhelp.query(hshopidsql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                  })
            }
            else{
                // console.log(result);
                response.json({
                    id:result,
                    status: 1
                  })
            }
        })
    })
    clientRouter.post("/hshopimg",(request,response,next)=>
    {
        let hshopidsql="SELECT hs_img,hs_name FROM hot_shop WHERE hs_id=?";
       let params=[request.body.hs_id]
    //    console.log(params)
        dbhelp.query(hshopidsql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                  })
            }
            else{
                // console.log(result);
                response.json({
                    img:result,
                    status: 1
                  })
            }
        })
    });
    clientRouter.post("/product",(request,response,next)=>
    {
        let productsql="SELECT p_name,p_img,p_price,p_id FROM hs_product WHERE hs_id=?";
       let params=request.body.hs_id;
       dbhelp.query(productsql,params,(err,result)=>
       {
           if(err)
           {
               response.json({
                   err
               })
           }
           else{
               response.json({
                   pro:result,
                   status:1
               })
           }
       })
    });
    clientRouter.post("/productid",(request,response,next)=>
{
    let params=request.body.p_id;
    // console.log(params)

    let productsql="SELECT * FROM hs_product WHERE p_id=?";
    dbhelp.query(productsql,params,(err,result)=>
    {
        if(err)
        {
            response.json({
                err
            })
        }
        else{
            response.json({
                val:result,
                status:1
            })
        }
    })
})
    clientRouter.post("/crowdpro",(request,response,next)=>
    {
        console.log(request.body.data);
        function fn1(cb)
        {
            let crowdsql="select * from `crowd_product`";
            dbhelp.query(crowdsql,[],(err,result)=>
            {
              //   console.log(result);
                cb(err,result);
            })
        }
        function fn2(cb)
        {
            let crowdremensql="SELECT * FROM `crowd_product` WHERE c_remen=1 and c_status=1 LIMIT 0,4";
            dbhelp.query(crowdremensql,[],(err,result)=>
            {
              //   console.log(result);
                cb(err,result);
            })
        }
        function fn3(cb)
        {
            let datasql="SELECT * FROM `crowd_product` WHERE CAST(startday AS SIGNED INTEGER)>? AND c_status=1 LIMIT 0,4";
            dbhelp.query(datasql,[request.body.data],(err,result)=>
            {
              //   console.log(result);
                cb(err,result);
            })
        }
        function fn4(cb)
        {
            let suoyou="SELECT * FROM `crowd_product`";
            dbhelp.query(suoyou,[],(err,result)=>
            {
              //   console.log(result);
                cb(err,result);
            })
        }
        async.parallel([fn1,fn2,fn3,fn4],(err,val)=>
      {
        //   console.log("val",val);
        if(err)
        {
            response,json({
                err
            })
        }
        else{
              response.json({val:val});
        }
      
      })
        // let crowdsql="select * from `crowd_product`";
        // dbhelp.query(crowdsql,[],(err,result)=>
        // {
        //     if(err)
        //     {
        //         response.json({
        //             err
        //           })
        //     }
        //     else{
        //         // console.log(result);
        //         response.json({
        //             crowd:result,
        //             status: 1
        //           })
        //     }
        // })
    })
    clientRouter.post("/crowdproductid",(request,response,next)=>
    {
        let params=[request.body.c_id];
      
             let crowdproductsql="SELECT * FROM crowd_product WHERE c_id=? AND c_status='1'"
             dbhelp.query(crowdproductsql,params,(err,reslut)=>{
                //  console.log(reslut)
               if(err)
               {
                   response.json({
                       err
                   })
               }
               else{
                   response.json({
                       reslut:reslut
                   })
               }
             }
             )
    })

    clientRouter.post("/addcart",(request,response,next)=>
    {
        // console.log(request.body.style)
        let res=request.body;
        let params1=[res.pid,res.uid,JSON.stringify(res.style)];
        let querysql="SELECT * FROM `cart` WHERE p_id=? AND u_id=?  AND c_style=?";
        dbhelp.query(querysql,params1,(err1,result1)=>{
            //   console.log(result1);
            if(err1)
            {
                response.json({
                    err1
                  })
            }
            else{
                if(result1.length>=1)
                {
                    let num=result1[0].c_num;
                    let endnum=parseInt(num)+parseInt(res.num);
                    let endprice=endnum*res.price;
                    let params2=[endnum,endprice,res.pid,res.uid,JSON.stringify(res.style)];
                    let updatesql=" UPDATE `cart` SET c_num=?,c_total=? WHERE p_id=? AND u_id=? AND c_style=?"
                    dbhelp.query(updatesql,params2,(err2,result2)=>{
                       if(err2)
                       {
                         response.json({
                            err2
                        })   
                       }
                       else{
                          if(result2.affectedRows>=1)
                          {
                            response.json({
                                                mm:"添加购物车成功",
                                                status: 1
                                              }) 
                          }
                       else{
                        response.json({
                                            mm:"添加购物车失败",
                                            status: 0
                                          })
                       }
                       } 
                    })
                }
                else{
                    let params=[res.pid,res.uid,res.name,res.img,res.price,res.num,JSON.stringify(res.style),res.total,res.company,res.uname];
                    // console.log(params);
                    let insertsql="INSERT INTO `cart` (p_id,u_id,c_name,c_img,c_price,c_num,c_style,c_total,c_company,u_name) VALUES (?,?,?,?,?,?,?,?,?,?)";
                    dbhelp.query(insertsql,params,(err,result)=>
                    {
                        // console.log(result);
                        if(err)
                        {
                            response.json({
                                err
                              })
                        }
                        else{
                            if(result.affectedRows>=1)
                            {
                                response.json({
                                    mm:"添加购物车成功",
                                    status: 1
                                  })
                            }
                            else{
                                response.json({
                                    mm:"添加购物车失败",
                                    status: 0
                                  })
                            }
                        }
                    })
                }  
            }
        });  
    })
    clientRouter.post("/cartnum",(request,response,next)=>
    {
        let params=[request.body.u_id];
        let cartnumsql="SELECT SUM(c_num) AS COUNT FROM `cart` WHERE u_id=?";
        dbhelp.query(cartnumsql,params,(err,result)=>
        {
            console.log(result[0].COUNT);
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                if(result[0].COUNT)
                {
                    response.json({
                num:result[0].COUNT
            }) 
                }
                else{
                    response.json({
                        num:0
                    })  
                }
            
            }
        })
    });
    clientRouter.post("/cart",(request,response,next)=>
    {
        // console.log(request.body)
        let params=[request.body.u_id]
        let cartsql="SELECT * FROM `cart` WHERE u_id=?";
        dbhelp.query(cartsql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                response.json({
                    result:result
                })
            }
            console.log("result",result);
        })
    });
    clientRouter.post("/delete",(request,response,next)=>
    {
        let params=request.body.c_id;
        let count=params.length;
  let arr=""
  for(let i=0;i<count;i++)
  {
    arr+=",?"
  }
  arr=arr.substring(1)
//   let delusersql=`DELETE FROM USER WHERE u_name IN (${arr})`;
  let deletesql=`DELETE FROM cart WHERE c_id IN (${arr})`;
        dbhelp.query(deletesql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                if(result.affectedRows>=1)
                {
                    response.json({
                        status:1,
                        msg:"删除成功"
                    })
                }
                else{
                    response.json({
                        status:0,
                        msg:"删除失败"
                    })
                }
            }
        })
    })
    clientRouter.post("/addcrowd",(request,response,next)=>
    {
        let params=[request.body.c_name,request.body.cs_img,request.body.c_detailes,request.body.c_targetmoney,request.body.c_day,request.body.max_yield,request.body.c_price,request.body.c_goodsday,request.body.c_content,request.body.c_company,request.body.c_imgs,request.body.c_fhtime,request.body.c_classify,request.body.cu_id,request.body.startday];
        
        let addcrowdsql="INSERT INTO crowd_product (`c_name`,`cs_img`,`c_detailes`,`c_targetmoney`,`c_day`,`max_yield`,`c_price`,`c_goodsday`,`c_content`,`c_company`,`c_imgs`,`c_fhtime`,`c_classify`,`cu_id`,`startday`) VALUE (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        dbhelp.query(addcrowdsql,params,(err,result)=>
        {
            console.log(result);
          if(err)
          {
              response.json({err})
          }
          else if(result.affectedRows>=1){
              response.json({
                 msg:"申请完成",
                 status:1
              })
          }
          else{
             response.json({
                msg:"申请失败",
                status:-1
             })
          }
        })
    })
    clientRouter.post("/viewcrowd",(request,response,next)=>
    {
        let params=[request.body.cu_id];
        console.log(request.body)
        let viewcrowdsql="SELECT * FROM `crowd_product` WHERE cu_id=?";
        dbhelp.query(viewcrowdsql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                // console.log(result.length);
                if(result.length==0)
                {
                    response.json({
                        msg:"您还没有发起任何众筹",
                        status:0
                    })
                }
                else{
                    response.json({
                        msg:"成功返回",
                        status:1,
                        result:result
                    })
                }
            }
        })
        
    })
    clientRouter.post("/topay",(request,response,next)=>
    {
        let params=[request.body.cc_price*request.body.cc_num,request.body.c_id];
        console.log(params);
        function fn1(cb)
        {
           let updatesql="UPDATE `crowd_product` SET c_arrive=c_arrive+?,c_support=c_support+1 WHERE c_id=?"
           dbhelp.query(updatesql,params,(err,reslut)=>{
               console.log("result",reslut)
              cb(err,reslut);
           }
           )
        }
        let params1=[request.body.c_id,request.body.u_id,request.body.cc_num,request.body.cc_name,request.body.cc_price,request.body.cc_img,request.body.cc_time,request.body.cc_address,request.body.cc_day,request.body.u_name,request.body.u_number];
        function fn2(cb)
        {
            let topaysql="INSERT INTO `cart_crowd` (c_id,u_id,cc_num,cc_name,cc_price,cc_img,cc_time,cc_address,cc_day,u_name,u_number) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
           dbhelp.query(topaysql,params1,(err,reslut)=>{
              //  console.log(reslut)
              cb(err,reslut);
           }
           )
        }
        async.parallel([fn1,fn2],(err,val)=>
      {
          if(err)
          {
              response.json({
                  err
              })
          }
          else{
            response.json({val:val});
          }
        //   console.log("val",val);
      })
        
    })

    clientRouter.post("/payed",(request,response,next)=>
    {
        let params=[request.body.cu_id];
        // console.log(request.body)
        let payedsql=" SELECT * FROM cart_crowd WHERE u_id=?";
        dbhelp.query(payedsql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                // console.log(result.length);
                if(result.length==0)
                {
                    response.json({
                        msg:"您还没有参与众筹",
                        status:0
                    })
                }
                else{
                    response.json({
                        msg:"成功返回",
                        status:1,
                        result:result
                    })
                }
            }
        })
        
    })
    clientRouter.post("/upstatus",(request,response,next)=>
    {
        let status="";
        // console.log(request.body)
        if(request.body.cc_status==1)
        {
            status=2
        }
        else if(request.body.cc_status==2)
        {
            status=3
        }
        else if(request.body.cc_status==0)
        {
            status=1
        }
        // console.log("会对发觉会否额去哈佛了去翻红了前高后切",status)
        let params=[status,request.body.cc_id];
        console.log(request.body)
        let payedsql="  UPDATE `cart_crowd` SET  cc_status=? WHERE cc_id=?";
        dbhelp.query(payedsql,params,(err,result)=>
        {
            console.log(result);
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                if(result.affectedRows>=1)
                {
                    response.json({
                        msg:"成功",
                        status1:1
                    })
                }
                else{
                    response.json({
                        msg:"失败",
                        status1:0
                    })
                }
            }
        })
        
    })

    clientRouter.post("/cartcrowd",(request,response,next)=>
{
   let size = request.body.limit * 1 // 页大小
//    console.log(request.body.limit);
   let start = (request.body.page - 1) * request.body.limit; 
   let params=[request.body.cu_id,start,size]
   console.log(params);
      function fn1(cb)
      {
         let getcartsql="SELECT * FROM `cart_crowd` INNER JOIN `crowd_product` ON `cart_crowd`.`c_id`=`crowd_product`.`c_id`"
         
        //  console.log(request.body)
         if(request.body.name)
         {
            params.unshift(request.body.name);
            // console.log(params)
            getcartsql += " WHERE LOCATE(?, `cc_name`) and `crowd_product`.`cu_id`=?"
         }
         else{
            getcartsql += " WHERE `crowd_product`.`cu_id`=?"

         }
         getcartsql+=" LIMIT ?,? ;"
        //  console.log(getcartsql);
         dbhelp.query(getcartsql,params,(err,reslut)=>{
        //   console.log(getcartsql);
            cb(err,reslut);
         }
         )
      }
      function fn2(cb)
      {
         let sqlcount="SELECT COUNT(*) AS count1  FROM `cart_crowd` INNER JOIN `crowd_product` ON `cart_crowd`.`c_id`=`crowd_product`.`c_id`";
         let params=[request.body.cu_id];
         if(request.body.name)
         {
            params.unshift(request.body.name);
            sqlcount += " WHERE LOCATE(?, `cc_name`) and `crowd_product`.`cu_id`=?";
         }
         else{
            sqlcount += " WHERE `crowd_product`.`cu_id`=?";
         }
         dbhelp.query(sqlcount,params,(err,result)=>
         {
            cb(err,result);
         })
      }

      setTimeout(()=>
      {
         async.parallel([fn1,fn2],(err,val)=>
         {
        //    console.log(val);
            let resultObj = {
               "code": 0,
               "msg": "",
               "count": val[1][0]["count1"],
               data: val[0]
           }
           response.json(resultObj);
         })
      
      },1000)
})
    
clientRouter.post("/addcomment",(request,response,next)=>
{
    let params=[request.body.c_id,request.body.u_id,request.body.u_name,request.body.pl_comment,request.body.pl_sex];
    // console.log(request.body)
    let addcommentsql="INSERT INTO `c_comment` (c_id,u_id,u_name,pl_comment,pl_sex) VALUES (?,?,?,?,?)";
    dbhelp.query(addcommentsql,params,(err,result)=>
    {
        if(err)
        {
            response.json({
                err
            })
        }
        else{
            // console.log(result);
            if(result.affectedRows>=1)
            {
                 response.json({
                     msg:"评论成功",
                     status:"success"
            })
            }
            else{
                response.json({
                    msg:"评论失败",
                    status:"warning"
                })
            }
           
        }
    })
    
}) 

clientRouter.post("/crowdcomment",(request,response,next)=>
    {
        let params=[request.body.c_id];
        // console.log(request.body)
        let payedsql=" SELECT * FROM c_comment WHERE c_id=?";
        dbhelp.query(payedsql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                // console.log(result.length);
                if(result.length==0)
                {
                    response.json({
                        msg:"暂时没有人评论",
                        status:0
                    })
                }
                else{
                    response.json({
                        msg:"成功返回",
                        status:1,
                        result:result
                    })
                }
            }
        })
        
    })

    clientRouter.post("/order",(request,response,next)=>
    {
        console.log(request.body)
    //     let str="",str1=""
    //     for(let i=0;i<request.body.pp_id.length;i++)
    //     {
    //         str+=" when "+request.body.pp_id[i]+" then p_stock-"+request.body.num[i];
    //         str1+=request.body.pp_id[i]+","
    //     }
    //    str1= str1.substring(0,str.length-2)
    //     console.log(str1)
    //     let jiansql="UPDATE `hs_product` SET p_stock = CASE p_id"+str+ " ENDWHERE p_id IN ("+str1+")";

    //     response.json({
    //         kkk:"kkk"
    //     })
        if(request.body.dec_id)
        {
            console.log(request.body)
        let params=request.body.dec_id;
        // console.log(params);
        let count=params.length;
        let arr=""
        for(let i=0;i<count;i++)
        {
          arr+=",?"
        }
        arr=arr.substring(1);
        function fn1(cb)
        {
           let somedetelesql=`DELETE FROM cart WHERE c_id IN (${arr})`
           dbhelp.query(somedetelesql,params,(err,reslut)=>{
               console.log("result",reslut)
              cb(err,reslut);
           }
           )
        }
        let params1=[request.body.o_content,request.body.u_id,request.body.o_number,request.body.o_name,request.body.o_address,request.body.o_data];
        function fn2(cb)
        {
            let addordersql="INSERT INTO `order` (o_content,u_id,o_number,o_name,o_address,o_data) VALUES (?,?,?,?,?,?)";
           dbhelp.query(addordersql,params1,(err,reslut)=>{
              //  console.log(reslut)
              cb(err,reslut);
           }
           )
        }
        function fn3(cb) {
            let str="",str1=""
            for(let i=0;i<request.body.pp_id.length;i++)
            {
                str+=" when "+request.body.pp_id[i]+" then p_stock-"+request.body.num[i];
                str1+=request.body.pp_id[i]+","
            }
            console.log(str1)
           str1= str1.substring(0,str1.length-1)
            console.log(str1)
            let jiansql="UPDATE `hs_product` SET p_stock = CASE p_id"+str+ " END WHERE p_id IN ("+str1+")";
            dbhelp.query(jiansql,[],(err,reslut)=>{
                //  console.log(reslut)
                cb(err,reslut);
             })
        }
        async.parallel([fn1,fn2,fn3],(err,val)=>
      {
          if(err)
          {
              response.json({
                  err
              })
          }
          else{
            if(val[0].affectedRows>=1&&val[1].affectedRows>=1&&val[2].affectedRows>=1)
            {
                response.json({
                    msg:"付款成功",
                    status:1
                })
            }
            else{
                response.json({
                    msg:"付款失败",
                    status:0
                })  
            }
          }
        //   console.log("val",val);
      })
        }
        else{
            function fn1(cb)
            {
               let params1=[request.body.o_content,request.body.u_id,request.body.o_number,request.body.o_name,request.body.o_address,request.body.o_data];
            let addordersql="INSERT INTO `order` (o_content,u_id,o_number,o_name,o_address,o_data) VALUES (?,?,?,?,?,?)";
            dbhelp.query(addordersql,params1,(err,reslut)=>{
                //  console.log(reslut)
                cb(err,reslut);
             }
        //    dbhelp.query(addordersql,params1,(err,reslut)=>{
        //       //  console.log(reslut)
        //       if(err)
        //       {
        //           response.json({
        //               err
        //           })
        //       }
        //       else{
        //         if(reslut.affectedRows>=1)
        //         {
        //             response.json({
        //                 msg:"付款成功",
        //                 status:1
        //             })
        //         }
        //         else{
        //             response.json({
        //                 msg:"付款失败",
        //                 status:0
        //             })  
        //         }
        //       }
            
        //    }
           ) 
            }
            function fn2(cb)
            {
                let str="",str1=""
                for(let i=0;i<request.body.pp_id.length;i++)
                {
                    str+=" when "+request.body.pp_id[i]+" then p_stock-"+request.body.num[i];
                    str1+=request.body.pp_id[i]+","
                }
                console.log(str1.length)
               str1= str1.substring(0,str1.length-1)
                console.log(str1)
                let jiansql="UPDATE `hs_product` SET p_stock = CASE p_id"+str+ " END WHERE p_id IN ("+str1+")";
                console.log("jian",jiansql) 
                dbhelp.query(jiansql,[],(err,reslut)=>{
                     console.log("reslut",reslut)
                    cb(err,reslut);
                 })
            }
            async.parallel([fn1,fn2],(err,val)=>
            {
                if(err)
                {
                    response.json({
                        err
                    })
                }
                else{
                  if(val[0].affectedRows>=1&&val[1].affectedRows>=1)
                  {
                      response.json({
                          msg:"付款成功",
                          status:1
                      })
                  }
                  else{
                      response.json({
                          msg:"付款失败",
                          status:0
                      })  
                  }
                }
              //   console.log("val",val);
            })
            
        
        }
        
        
    })

    clientRouter.post("/getorder",(request,response,next)=>
    {
        let params=[request.body.u_id];
        // console.log(request.body)
        let getordersql="SELECT * FROM `order` WHERE u_id=?";
        dbhelp.query(getordersql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                // console.log(result.length);
                if(result.length==0)
                {
                    response.json({
                        msg:"还没购买过任何商品",
                        status:0
                    })
                }
                else{
                    response.json({
                        msg:"成功返回",
                        status:1,
                        result:result
                    })
                }
            }
        })
        
    })

    clientRouter.post("/updateorder",(request,response,next)=>
    {
        let params=[request.body.o_content,request.body.o_id];
        // console.log(request.body)
        let updateordersql="  UPDATE `order` SET  o_content=? WHERE o_id=?";
        dbhelp.query(updateordersql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                console.log(result);
                if(result.affectedRows>=1)
                {
                     response.json({
                   msg:"success",
                   status:1
               })
                }
                else{
                    response.json({
                        msg:"fail",
                        status:0
                    })
                }
            //    console.log(result);
              
            }
        })
        
    })

  clientRouter.post("/commentorder",(request,response,next)=>
    {
        let params=[request.body.u_id,request.body.p_id,request.body.u_sex,request.body.u_name,request.body.datass,request.body.spl_content];
        function fn1(cb)
        {
           let updatesql="INSERT INTO `comment` (u_id,p_id,u_sex,u_name,datas,spl_conent) VALUES (?,?,?,?,?,?)"
           dbhelp.query(updatesql,params,(err,reslut)=>{
               console.log("result",reslut)
              cb(err,reslut);
           }
           )
        }
        let params1=[request.body.o_content,request.body.o_id];
        function fn2(cb)
        {
            let updateorder=" UPDATE `order` SET  o_content=? WHERE o_id=?";
           dbhelp.query(updateorder,params1,(err,reslut)=>{
              //  console.log(reslut)
              cb(err,reslut);
           }
           )
        }
        async.parallel([fn1,fn2],(err,val)=>
      {
          if(err)
          {
              response.json({
                  err
              })
          }
          else{
            response.json({val:val});
          }
        //   console.log("val",val);
      })
        
    })

    clientRouter.post("/comment",(request,response,next)=>
    {
        let params=[request.body.p_id];
        // console.log(request.body)
        let payedsql=" SELECT * FROM `comment` WHERE p_id=?";
        dbhelp.query(payedsql,params,(err,result)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
                // console.log(result.length);
                if(result.length==0)
                {
                    response.json({
                        msg:"暂时没有人评论",
                        status:0
                    })
                }
                else{
                    response.json({
                        msg:"成功返回",
                        status:1,
                        result:result
                    })
                }
            }
        })
        
    })


    clientRouter.post("/search",(request,response,next)=>
    {
        console.log(request.body)
        function fn1(cb)
        {
            let params=[];
             let searchsql=" SELECT count(*) as counts FROM `hs_product`";
        if(request.body.searchs.length==0)
        {
             params=[]
        }
        else{
            searchsql+=" WHERE LOCATE(?,p_name)";
             params=[request.body.searchs];
        }
        dbhelp.query(searchsql,params,(err,reslut)=>{
            // console.log("result",reslut)
           cb(err,reslut);
        })}
        function fn2(cb)
        {
            let params=[];
             let searchsql=" SELECT * FROM `hs_product`";
        if(request.body.searchs.length==0)
        {
             params=[]
        }
        else{
            searchsql+=" WHERE LOCATE(?,p_name)";
             params=[request.body.searchs];
        }
        dbhelp.query(searchsql,params,(err,reslut)=>{
            // console.log("result",reslut)
           cb(err,reslut);
        })}
        async.parallel([fn1,fn2],(err,val)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
              response.json({val:val});
            }
          //   console.log("val",val);
        })
        
       
        
        
    })


    clientRouter.post("/zongcrowd",(request,response,next)=>
    {
        console.log(request.body.c_classify)
        let params=[request.body.c_classify];
        function fn1(cb)
        {
             let zongsql=" SELECT count(*) as counts FROM `crowd_product` where c_classify=?";
        dbhelp.query(zongsql,params,(err,reslut)=>{
            // console.log("result",reslut)
           cb(err,reslut);
        })}
        function fn2(cb)
        {
             let zongcrowdsql=" SELECT * FROM `crowd_product` where c_classify=?";
        dbhelp.query(zongcrowdsql,params,(err,reslut)=>{
            // console.log("result",reslut)
           cb(err,reslut);
        })}
        async.parallel([fn1,fn2],(err,val)=>
        {
            if(err)
            {
                response.json({
                    err
                })
            }
            else{
              response.json({val:val});
            }
          //   console.log("val",val);
        })
        
       
        
        
    })

  module.exports = clientRouter;